package com.pos.clothes.controller;

import com.pos.clothes.pojo.Discount;
import com.pos.clothes.service.DiscountService;
import com.pos.clothes.util.JsonData;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller

@RequiresPermissions("2")
public class DiscountController {
    @Autowired
    public DiscountService service;

    @RequestMapping("/getAllDiscount")
    @ResponseBody
    public HashMap<String ,Float> getAllDiscount(){
        Discount discount = service.getAllDiscount();
        HashMap<String,Float> hashMap = new HashMap<String,Float>();
        hashMap.put("alldiscount", discount.getAll_rule());
        return hashMap;
    };
    @RequestMapping("/getBorderDiscount")
    @ResponseBody
    public List<Discount> getBorderDiscount(){
        List<Discount> discount = service.getBorderDiscount();
        return discount;
    }
    @RequestMapping("/updateAllDiscount")
    @ResponseBody
    public Object updateAllDiscount(@RequestParam("allrule") float allrule){
        Discount discount = new Discount();
        discount.setAll_rule(allrule);
        int data = service.updateAllDiscount(discount);
        if (data==1){
            return JsonData.buildSuccess(data,"更改成功");
        }
        return JsonData.buildError("更改失败");
    };
    @RequestMapping("/updateBorderDiscount")
    @ResponseBody
    public Object updateBorderDiscount(@RequestParam("border1") int border1,
                                        @RequestParam("border2") int border2,
                                        @RequestParam("border3") int border3,
                                        @RequestParam("border_rule1") int border_rule1,
                                        @RequestParam("border_rule2") int border_rule2,
                                        @RequestParam("border_rule3") int border_rule3){
        List<Discount> discounts = new ArrayList<>();

        Discount discount1 = new Discount();
        discount1.setIddiscount(2);
        discount1.setBorder(border1);
        discount1.setBorder_rule(border_rule1);
        discounts.add(discount1);

        Discount discount2 = new Discount();
        discount2.setIddiscount(3);
        discount2.setBorder(border2);
        discount2.setBorder_rule(border_rule2);
        discounts.add(discount2);

        Discount discount3 = new Discount();
        discount3.setIddiscount(4);
        discount3.setBorder(border3);
        discount3.setBorder_rule(border_rule3);
        discounts.add(discount3);
        return service.updateBorderDiscount(discounts);
    };
}
